/*
 * ExceptionIntercepter.java
 * 作者：IccBoY
 * 2013-8-11 创建文件
 */
package com.iccboy.demo.basic.exception;

import org.omg.CORBA.portable.ApplicationException;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

/**
 ************************************************** 
 * 异常拦截器
 * 
 * @version 0.0.1
 * @author IccBoY
 ************************************************* 
 */
public class ExceptionIntercepter extends MethodFilterInterceptor {

	private static final long serialVersionUID = 6574291456516165785L;

	protected String doIntercept(ActionInvocation actionInvocation)
			throws Exception {
		ActionSupport actionSupport = (ActionSupport) actionInvocation
				.getAction();
		try {
			String returnStr = actionInvocation.invoke();
			return returnStr;
		} catch (Exception e) {
			return exception(e, actionSupport);
		}
	}

	private String exception(Exception e, ActionSupport actionSupport)
			throws Exception {
		if (e instanceof ApplicationException) {
			String msg = ExceptionHandler.getExceptionMessage(e);
			actionSupport.addActionMessage(msg);
			return Action.INPUT;
		} else {
			String msg = ExceptionHandler.getExceptionMessage(e);
			actionSupport.addActionMessage(msg);
			return Action.ERROR;
		}
	}

}
